﻿package com.totallypracticalgames.unityflashbridge
{
	import adobe.utils.CustomActions;
	import flash.utils.Dictionary;
	
	/**
	 * ...
	 * @author Roger Clark
	 */
	public class UnityFlashIntegrator extends Object 
	{
		public static const CHILD_READY:String = "UNITY_FLASH_BRIDGE_CHILD_READY";
		public static const CHILD_START:String = "UNITY_FLASH_BRIDGE_CHILD_START";
		public static const CHILD_EXIT:String = "UNITY_FLASH_BRIDGE_CHILD_EXIT";
		static private var _instance:UnityFlashIntegrator;
		public var unityVars:Object=null;
		
		
		/**
		 * [Singleton] Creates a new instance of the UnityFlash class
		 */
		public function UnityFlashIntegrator(enforcer:CommonSingletonEnforcer)
		{
			if (!enforcer)
			{
				throw new Error("UnityFlash is a Singleton and cannot be directly instantiated. Use Common.getInstance().");
			}
			else
			{
				unityVars = new Object();
			}
		}
		
		// STATIC PUBLIC FUNCTIONS
		// ------------------------------------------------------------------------------------------
		/**
		 * Retrieves the single instance of Common class
		 * @return	The single instance of the Common class
		 */
		static public function getInstance () : UnityFlashIntegrator
		{
			if (!_instance) _instance = new UnityFlashIntegrator(new CommonSingletonEnforcer());
			return _instance;
		}		
		

		/**
		 * Sets a "variable" in the unityVars object 
		 * @return	Boolean: true if successful. flase if variable was not already in the table.
		 */
		public function setVar(varName:String, varValue:*):Boolean
		{
			if (unityVars[varName])
			{
				// var already exists, so update it
				unityVars[varName] = varName;
				return(true);
			}
			else
			{
				return(false);
			}
		}
		
		/**
		 * Gets a "variable" in the unityVars object 
		 * @return	the value of the variable or null if it doesn't exist
		 */
		public function getVar(varName:String):*
		{
			return(unityVars[varName]);
		}	
		
		/**
		 * Called by client (child) swf when its ready to start
		 * @return	nothing
		 */
		public function clientReady():void
		{
			
		}
		
		/**
		 * Called by client (child) swf when its ready to quit and return control to Unity
		 * @return	nothing
		 */
		public function clientExit():void
		{
		}
		
	}
}

/**
 * A private class used to enforce the Common class as a Singleton
 */
 class CommonSingletonEnforcer { }
